package org.jsola.hr.web;

import org.apache.commons.io.FilenameUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpSocInfoAddBatchDTO;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.query.*;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.service.IEmpSocTransformService;
import org.jsola.hr.vo.*;
import org.jsola.hr.vo.excel.FieldItemGroupVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @author lyh
 * <p>
 * 员工社保状态改变控制层
 */
@RestController("hrEmpSocTransformController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSocTransform")
public class EmpSocTransformController implements IEmpSocTransformControllerAPI {

    @Autowired
    private IEmpSocTransformService empSocTransformService;

    @Autowired
    private IEmpInfoService empInfoService;

    @Override
    @PostMapping(value = "/fillGather")
    public Result<Integer> fillGather(@Valid @RequestBody FillSocQuery fillSocQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        empSocTransformService.fillGather(fillSocQuery, tokenUser);
        return Result.success();
    }

    @Override
    @PostMapping(value = "/fillDifference")
    public Result<String> fillDifference(@Valid @RequestBody FillSocQuery fillSocQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验


        return Result.success(
                empSocTransformService.fillDifference(fillSocQuery, tokenUser)
        );
    }

    @Override
    @PostMapping(value = "/pfFillPayment")
    public Result<Integer> pfFillPayment(@Valid @RequestBody FillSocQuery fillSocQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        empSocTransformService.pfFillPayment(fillSocQuery, tokenUser);
        return Result.success();
    }

    @Override
    @GetMapping(value = "/selectEmpCountWithSocProgId")
    public Result<SocProgCountAndNextMonthVO> selectEmpCountWithSocProgId(@RequestParam("socProgId") String socProgId,
                                                                          @RequestParam("companyId") String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectEmpCountWithSocProgId(socProgId, companyId, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "/previewFill")
    public Result<List<SocFillPreviewVO>> previewFill(@Valid @RequestBody PreviewFillQuery previewFillQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看参保信息表权限校验
        return Result.success(
                empSocTransformService.previewFill(previewFillQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectAllHead/{companyId}")
    public Result<List<FieldItemGroupVO>> selectAllHead(@PathVariable String companyId,
                                                        @RequestParam(value = "monthId", required = false) String monthId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectAllHead(companyId, monthId, tokenUser)
        );
    }

    @Override
    @PostMapping("/export")
    public ResponseEntity<Resource> export(@Valid @RequestBody EmpSocTransformQuery empSocTransformQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验
        String excelFilePath = empSocTransformService.excelExport(empSocTransformQuery, tokenUser);
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @PostMapping("/exportEmpSocInfo")
    public ResponseEntity<Resource> exportEmpSocInfo(@Valid @RequestBody EmpSocInfoQuery empSocInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验
        String excelFilePath = empSocTransformService.empSocInfoExport(empSocInfoQuery, tokenUser);
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @GetMapping("/empSocDetailsExport")
    public ResponseEntity<Resource> empSocDetailsExport(@RequestParam("companyId") String companyId,
                                                        @RequestParam("empId") String empId,
                                                        @RequestParam(value = "empSocInfoIdList", required = false) List<String> empSocInfoIdList) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验
        String excelFilePath = empSocTransformService.empSocDetailsExport(companyId, empId, empSocInfoIdList, tokenUser);
        EmpInfoDO empInfoDO = empInfoService.selectDOById(empId, tokenUser.getSiteId());
        // 编码文件名，防止乱码
        String filename = empInfoDO.getName() + "参保记录.xls";
        String filenames = new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @PostMapping(value = "/selectEmpSocInfoByEmpId")
    public Result<Page<EmpSocTransformVO>> selectEmpSocInfoByEmpId(@Valid @RequestBody EmpSocTransformQuery empSocTransformQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectEmpSocInfoByEmpId(empSocTransformQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectEmpSocList")
    public Result<Page<EmpSocTransformVO>> selectEmpSocList(@Valid EmpSocTransformQuery empSocTransformQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectEmpSocList(empSocTransformQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectEmpSocCount")
    public Result<EmpSocTransformCountVO> selectEmpSocCount(EmpSocInfoQuery empSocInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectEmpSocCount(empSocInfoQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectEmpSocListCount")
    public Result<EmpSocTransformCountVO> selectEmpSocListCount(@RequestParam("companyId") String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectEmpSocListCount(companyId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectAllMantissaProcessingType")
    public Result<Map<String, Integer>> selectIncreaseOrDecreaseListCount() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectAllMantissaProcessingType()
        );
    }

    @Override
    @GetMapping(value = "/getEmpSocListQueryEnum")
    public Result<EmpSocListQueryEnumVO> getEmpSocListQueryEnum(String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        return Result.success(
                empSocTransformService.getEmpSocListQueryEnum(companyId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectEmpSocInfoByEmpIdCount")
    public Result<EmpSocTransformCountVO> selectEmpSocInfoByEmpIdCount(@RequestParam("companyId") String companyId,
                                                                       @RequestParam("empId") String empId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectEmpSocInfoByEmpIdCount(companyId, empId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectEmpSocBriefly")
    public Result<EmpSocBrieflyVO> selectEmpSocBriefly(@Valid @RequestParam("empId") String empId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工参保信息表权限校验

        return Result.success(
                empSocTransformService.selectEmpSocBriefly(empId, tokenUser)
        );
    }

}
